<?php

namespace app\index\controller;

use think\Controller;
use think\Db;
use think\Request;
use think\Session;
use think\Cookie;
use app\common\func\allRequest;

class Personal extends Controller
{
    public function index()
    {
    	!empty(session::get('user'))? $uid = session::get('user.id') : $uid = cookie::get('loginid');
    	//购物车里的商品数量
        $list = Db::field('d.name,d.face,o.pay_money,d.author')
                ->table(['s75_data'=>'d','s75_orders'=>'o'])
                ->where('d.id=o.cid')->where('o.status=0')->where('o.uid='.$uid)
                ->select();
        $this->assign('count',count($list));
        //查看用户登录信息
    	$user = Db::table('s75_user')->where('id',$uid)->find();
    	$this->assign('user',$user);
      $res = $this->callapi();
      $this->assign('newslist',$res);
    	return $this->fetch('personal/index');
    }

    public function userinfo(Request $request)
    {
       $data = $request->post();
       $result = [
          'name' => $data['name'],
          'tel'=>$data['tel'],
          'sex'=>$data['sex']
       ];

       $res = Db::table('s75_user')->where('id',$data['id'])->update($result);
       if($res){
               $info['status'] = 'true';
               $info['info'] = '保存成功';
       }else{
               $info['status'] = 'false';
               $info['info'] = '保存失败';
       }

       return json($info);
    }

    public function pwd(Request $request)
    {
       $data = $request->post();
       if(empty($data['password'])){
         $info['status'] = 'false';
          $info['info'] = '请输入密码';
          return json($info);
          exit;
       }
       if(empty($data['repassword'])){
         $info['status'] = 'false';
          $info['info'] = '请输入确认密码';
          return json($info);
          exit;
       }
      
       $preg = '/^(\d|\w){6,12}$/';
       $preg_match = preg_match($preg, $data['password']);
       if(!$preg_match){
          $info['status'] = 'false';
          $info['info'] = '请输入6-12位由数字，字母，下划线组成的密码';
          return json($info);
          exit;
       }

       if($data['password'] != $data['repassword']){
          $info['status'] = 'false';
          $info['info'] = '两次密码不一致';
          return json($info);
          exit;
       }else{
       unset($data['repassword']);
       $result = [
          'password' => md5($data['password'])
       ];

       $res = Db::table('s75_user')->where('id',$data['id'])->update($result);
       if($res){
              $info['status'] = 'true';
              $info['info'] = '修改成功';
       }else{
             $info['status'] = 'false';
            $info['info'] = '修改失败';
       }
     }

       return json($info);
    }


    public function icon(Request $request)
    {
        $result = $request->post();
        $key = key($_FILES);
        if(empty($key)){
            $this->error('上传文件过大');
        }

        $error = $_FILES[$key]['error'];
        if($error != 0){
            switch ($error) {
                case '1':$this->error('文件过大');break;
                case '2':$this->error('文件过大');break;
                case '3':$this->error('网络不稳定');break;
                case '4':$this->error('请选择文件');break;
                case '6':$this->error('服务器繁忙');break;
                case '7':$this->error('服务器繁忙');break;
                
            }
        }

        $tmpname = $_FILES[$key]['tmp_name'];
        if(!is_uploaded_file($tmpname)){
            $this->error('非法上传');
        }

        $allowtype = ['image'];
        $usertype = strstr($_FILES[$key]['type'],'/',true);
        if(!in_array($usertype,$allowtype)){
            $this->error('上传格式不正确');
        }

        $savedir = 'uploads'.date('Y/m/d/');
        if(!file_exists($savedir)){
            mkdir($savedir,0777,true);
        }

        $suffix = strrchr($_FILES[$key]['name'],'.');
        $filename = date('Ymd').uniqid().mt_rand(1000,9999).$suffix;

        if(move_uploaded_file($tmpname,$savedir.$filename)){
             $data = [
                'icon'=>'/'.$savedir.$filename
             ];
             $res = Db::table('s75_user')->where('id',$result['id'])->update($data);
             if($res){
                  $info['status'] = 'true';
                  $info['info'] = '更换成功';
             }
        }else{
                  $info['status'] = 'false';
                  $info['info'] = '更换失败';
        }

        return json($info);

    }

    public function aicon(Request $request)
    {
       !empty(session::get('user'))? $uid = session::get('user.id') : $uid = cookie::get('loginid');
       $data = $request->post();
       $res = [
          'icon'=>$data['pro']
       ];

       $result = Db::table('s75_user')->where('id',$uid)->update($res);
       if($result){
              $info['status'] = 'true';
              $info['info'] = '更换成功';
       }else{
              $info['status'] = 'false';
              $info['info'] = '更换失败';
       }
       return json($info);

    }

     public function callapi()
    {
        $url = "http://api.tianapi.com/social/?key=87d1c2233aa27fc61f1f6ca95d629f32&num=5";
        $urlreq = new allRequest(); 
        $msg = $urlreq->curl_request($url);
        return json_decode($msg)->newslist;
    }
}
